package br.com.tiespec.web.ManagedBean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

@Named("loginUnic")
@ApplicationScoped
public class LoginUnic implements Serializable {
	private static final long serialVersionUID = -7096314582515283859L;

	private static Map<Long, HttpSession> logins = new HashMap<Long, HttpSession>();

	public static Map<Long, HttpSession> getLogins() {
		return logins;
	}

	public static void setLogins(Map<Long, HttpSession> logins) {
		LoginUnic.logins = logins;
	}

	public static void login(Long user, HttpSession session) {

		HttpSession anterior = logins.remove(user);
		if (anterior != null) {
			try {
				anterior.setAttribute("deslogado", true);
			} catch (IllegalStateException e) {
				System.err.println("LoginUnic: sessão inválida! " + e.getMessage());
				e.printStackTrace();
			}
		}
		session.setAttribute("deslogado", false);
		logins.put(user, session);
	}
}